package org.libarae.communication.response;

import java.io.ByteArrayOutputStream;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.libarae.communication.XmlParseException;
import org.libarae.communication.common.Book;
import org.libarae.communication.common.Type;
import org.libarae.communication.common.User;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class ModeratorPanelResponse extends Response{
	
	public static final int STATUS_FAILED_USER_NOT_MODERATOR = 3;
	
	private Vector<User> mUsers = new Vector<User>();
	private Vector<Book> mBooks = new Vector<Book>();
	
	public ModeratorPanelResponse(int status){
		super(Type.MODERATOR_PANEL, status);		
	}
	
	public ModeratorPanelResponse(int status, Vector<User> users, Vector<Book> books){
		super(Type.MODERATOR_PANEL, status);	
		setUsers(users);
		setBook(books);
	}	
	
	@Override
	public String createXML() throws XmlParseException {
		Document doc;
		try{
			//setup the factory to build the xml
			DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
			doc = documentBuilder.newDocument();
		}
		catch (Exception e) {
			throw new XmlParseException("Error creating XML Builder",e);
		}
		
		//create the xml
		Element rootElement = doc.createElement("type");
		rootElement.setAttribute("typeID", super.getType()+"");
		doc.appendChild(rootElement);
		//user name
		Element node = doc.createElement("mStatus");
		node.setTextContent(getStatus()+"");
		rootElement.appendChild(node);

		//put vector in to the xml
		node = doc.createElement("mUsers");
		for(User u : mUsers){
			Element subNode =  doc.createElement("User");
			u.createXml(doc, subNode);
			node.appendChild(subNode);
		}	
		rootElement.appendChild(node);
		
		//put vector in to the xml
		node = doc.createElement("mBooks");
		for(Book b : mBooks){
			Element subNode =  doc.createElement("Book");
			b.createXml(doc, subNode);
			node.appendChild(subNode);
		}	
		rootElement.appendChild(node);
		
		//export the xml to a string
		String output = null;
		try {
			ByteArrayOutputStream byteArrayOutput = new ByteArrayOutputStream();
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(byteArrayOutput);
			transformer.transform(source, result);
			output = byteArrayOutput.toString();
		}
		catch (Exception e) {
			throw new XmlParseException("Error outputting XML to string", e);
		}
		
		//return the string
		return output;
	}

	@Override
	public void decodeXML(Document document) {
		// get the document root and node list
		Element root = document.getDocumentElement();
		NodeList nodeList = root.getChildNodes();

		// set all of the nodes in their proper places
		for (int i = 0; i < nodeList.getLength(); i++) {
			// find what node we have
			String nodeName = nodeList.item(i).getNodeName();
			if (nodeName.equals("mStatus")) {
				setStatus(Integer.parseInt(nodeList.item(i).getTextContent()));
			}
			else if (nodeName.equals("mBooks")) {
				NodeList nodeVector = nodeList.item(i).getChildNodes();
				mBooks = new Vector<Book>();
				for(int p = 0; p < nodeVector.getLength(); p++){
					Book book = new Book();
					book.decodeXML(nodeVector.item(p));					
					mBooks.add(book);
				}
			}
			else if (nodeName.equals("mUsers")) {
				NodeList nodeVector = nodeList.item(i).getChildNodes();
				mUsers = new Vector<User>();
				for(int p = 0; p < nodeVector.getLength(); p++){
					User user = new User();
					user.decodeXML(nodeVector.item(p));					
					mUsers.add(user);
				}
			}
			else {
				System.err.println("Unknow field in object XML: " + nodeName);
			}
		}	
	}

	public Vector<User> getUsers() {
		return mUsers;
	}

	public void setUsers(Vector<User> mUsers) {
		if(mUsers == null)
			this.mUsers = new Vector<User>();
		else
			this.mUsers = mUsers;
	}

	public Vector<Book> getBook() {
		return mBooks;
	}

	public void setBook(Vector<Book> mBook) {
		if(mBook == null)
			this.mBooks = new Vector<Book>();
		else
			this.mBooks = mBook;
	}

	@Override
	public String createHTML() {
		// TODO Auto-generated method stub
		return null;
	}
}
